package com.uvt.agenda.adapter;

import com.uvt.agenda.Event;
import com.uvt.agenda.Holiday;
import org.joda.time.DateTime;
import android.database.Cursor;



/**
 * Created with IntelliJ IDEA.
 * User: Cristi C
 * Date: 2/6/14
 * Time: 4:32 AM
 */
public class EventAdapter extends Event {
    private static Event holidayToEvent(Holiday holiday) {

        Event event = new Event();
        event.setID(holiday.getId());
        event.setName(holiday.getName());
        event.setDescription(holiday.getDescription());
        DateTime date = new DateTime(holiday.getYear(), holiday.getMonth(), holiday.getDay(), 0, 0);
        event.setStartDate(date.getMillis());
        event.setEndDate(date.getMillis());
        return event;
    }

    /**
     * Convert database entry to Event (from cursor)
     */
    private static Event cursorToEvent(Cursor cursor) {
        Event event = new Event();
        event.setID(cursor.getInt(0));
        event.setName(cursor.getString(1));
        event.setDescription(cursor.getString(2));
        event.setStartDate( cursor.getLong(3));
        event.setEndDate( cursor.getLong(4));
        return event;
    }

    public static Event toEvent(Object o){
        if(o instanceof Holiday){
            return holidayToEvent((Holiday)o);
        }
        else if(o instanceof Cursor){
            return cursorToEvent((Cursor)o);
        }
        else return null;
    }



}
